package com.hsj.demo.controller;

import com.hsj.demo.entity.User;
import com.hsj.demo.service.UserFriendService;
import com.hsj.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin(allowCredentials ="true")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserFriendService userFriendService;

    @RequestMapping("/login")
    public User login(User user, HttpSession session){
        User user1= userService.findByUsername(user.getUsername());
        if(user1!=null&&user1.getPassword().equals(user.getPassword())&& user1.getRole().equals(user.getRole())){
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(user1.getSealTime().after(date)){
                user1.setId(null);
                return user1;
            }
            session.setAttribute("user",user1);
            return user1;
        }
        return null;
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "login";
    }

    @RequestMapping("/register")
    public String register(User user){
        return userService.register(user);
    }

    @RequestMapping("/editProfile")
    public User editProfile(MultipartFile file, User newUser,HttpSession session){
        User oldUser = (User) session.getAttribute("user");
        User user = userService.editProfile(file,newUser,oldUser);
        session.setAttribute("user", user);
        return user;
    }

    @RequestMapping("/updatePassword")
    public String updatePassword(String oldPassword,String newPassword,HttpSession session){
        User user = (User)session.getAttribute("user");
        return userService.updatePassword(oldPassword,newPassword,user);
    }

    @RequestMapping("/findUserByMessage")
    public List<User> findUserByMessage(User user){
        List<User> userList = userService.findByEntity(user);
        return userList;
    }

    /**
     * 查询审核中用户
     * @param session
     * @return
     */
    @RequestMapping("/findAuditUser")
    public List<User> findAuditUser(HttpSession session,User user){
        User user1 = (User)session.getAttribute("user");
        if(user1.getRole()==2){
            return userService.findByEntity(user);
        }else{
            return null;
        }
    }

    /**
     * 根据查询条件查询审核中用户
     * @param session
     * @return
     */
    @RequestMapping("/findUserByEntity")
    public List<User> findUserByEntity(HttpSession session,User user){
        User user1 = (User)session.getAttribute("user");
        return userService.findByEntity(user);
    }

    /**
     * 修改用户审核状态
     * @param session
     * @param user
     * @return
     */
    @RequestMapping("/editAuditUser")
    public String editAuditUser(HttpSession session,User user){
        User user1 = (User)session.getAttribute("user");
        if(user1.getRole()==2){
            userService.update(user);
            return "修改成功";
        }else{
            return null;
        }
    }

    @RequestMapping("/update")
    public Integer update(User user){
        return userService.update(user);
    }

    @RequestMapping("/getById")
    public User getById(Integer id){
        return userService.findById(id);
    }

    @RequestMapping("/findByUsername")
    public User findByUsername(HttpSession session,String username){
        User user1 = (User)session.getAttribute("user");
        User user = userService.findByUsername(username);
        if(userFriendService.findUserFriendId(user1.getId(),user.getId())!=null){
            return user;
        }
        return null;
    }
}
